package cn.hibernatedemo.dao;

import cn.hibernatedemo.entity.Dept;
import org.hibernate.Query;
import org.hibernate.Session;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

public class DeptDao {
    public Dept get(Integer id){
        return (Dept) HibernateUtil.currentSession().load(Dept.class,id);
    }

    public void add(Dept dept){
        HibernateUtil.currentSession().save(dept);
    }

    public Dept load(Serializable id){
        return (Dept)HibernateUtil.currentSession().load(Dept.class,id);
    }
    public void delete(Dept d){
        HibernateUtil.currentSession().delete(d);
    }
    public Iterator<Dept> getList(){
        Session session=HibernateUtil.currentSession();
        String hql="from Dept";
        Query query=session.createQuery(hql);
        Iterator<Dept> depts=query.iterate();
        return depts;
    }

    public List<Dept> findDeptName(String deptname){
        String hql="from Dept where deptName=:name and location=:loc";
        Query query=HibernateUtil.currentSession().createQuery(hql);
        query.setString("name",deptname);
        query.setString("loc","郑州");
        return query.list();
    }
    public List<Dept> findDeptObjs(Object[] objs){
        String hql="from Dept where deptName=? and location=?";
        Query query=HibernateUtil.currentSession().createQuery(hql);
        if(objs.length>0&&objs!=null){
            for (int i = 0; i <objs.length ; i++) {
                query.setParameter(i,objs[i]);
            }
        }
        return query.list();
    }

    public List<Dept> findDeptObj(Dept objs){
        String hql="from Dept where deptName=:deptName and location=:location";
        Query query=HibernateUtil.currentSession().createQuery(hql);
        query.setProperties(objs);
        return query.list();
    }


}
